package com.neuronrobotics.opengl.scene;

import java.util.Arrays;

import android.opengl.Matrix;

import com.neuronrobotics.replicator.gui.stl.GeneralTransform3D.MatrixOrder;

public class TransformGroupNode extends GroupNode implements Transformable{

	private float modelTransform[]; // Individual rotational/translation matrix
	private MatrixOrder theMatrixOrder;	
	
	public TransformGroupNode(){
		super();
		modelTransform = new float[16];
		Matrix.setIdentityM(modelTransform, 0);
		theMatrixOrder = MatrixOrder.COLUMN_MAJOR;
	}
	
	public TransformGroupNode(GroupNode parentNode) {
		this();
		setParent(parentNode);
	}

	@Override
	public boolean setTransform(float[] newTransform, MatrixOrder matrixOrder) {
		if(newTransform.length<16) return false;
		modelTransform = Arrays.copyOf(newTransform, 16);
		theMatrixOrder = matrixOrder;
		return true;
	}

	@Override
	public boolean getTransform(float[] resultTransform, MatrixOrder matrixOrder) {
		if(resultTransform.length<16) return false;
		for(int i=0;i<16;i++) resultTransform[i] = modelTransform[i];
		if(this.theMatrixOrder!=matrixOrder){
			MatrixOrder.convertOrder(resultTransform, 0, resultTransform, 0);
		}		
		return true;
	}
	
	@Override
	protected void draw(float[] viewMatrix,float[] projMatrix){
		for(SceneNode node:children){
			node.setTransform(modelTransform, theMatrixOrder);
		}			
		super.draw(viewMatrix, projMatrix);
	}

		
}
